/*
 * Main Transaction object - what is stored in the IXRecordManagers
 *
 * Author: Kris Younger, NeXT Systems Engineering
 * You may freely copy, distribute and reuse the code in this example.  
 * NeXT disclaims any warranty of any kind, expressed or implied, as to 
 * its fitness for any particular use.
 */
/* Mostly Generated By SetGet - version 1.0 - kyounger */

#import "Transaction.h"
#import <appkit/Application.h>
#import "MainDelegate.h"
#import	<regex.h>

@interface Transaction(PRIVATE)
- (id)setTSerial:(int)tS;
@end

@implementation Transaction
- init
{
    char	number[16];

    tSerial = [[NXApp delegate] consumeSerial];
    if (tSerial == 0) {
	tSerial = -1;
    };
    tDate = [[NXApp delegate] todaysDate];
    sprintf(number, "%d", tSerial);
    tNumber = NXCopyStringBuffer(number);
    return self;
}

- free
{
    if (tNumber != NULL) { free(tNumber); tNumber = NULL; }
    if (tMemo != NULL) { free(tMemo); tMemo = NULL; }
    if (tDebit != NULL) { free(tDebit); tDebit = NULL; }
    if (tCredit != NULL) { free(tCredit); tCredit = NULL; }
    if (tBalance != NULL) { free(tBalance); tBalance = NULL; }
    return[super free];
}

- commit
{
    int                 rmHandle; /* not used*/
    id                  storeHandle;

    if ((recordManager != NULL) && (recordManager != nil)) {
	[(id)recordManager getBlock:&rmHandle andStore:&storeHandle];
	[storeHandle startTransaction];
	[recordManager replaceRecord:[self handle] with:self];
	[storeHandle commitTransaction];
    };
    return self;
}

- (unsigned int)handle
{
    return runTimeHandle;
}

- (int)tSerial
{
    return ((int)tSerial);
}

/* this should be a private method. */
- (id)setTSerial:(int)tS
{
    tSerial = tS;
    return self;
}

- (const char *)tNumber
{
    return ((const char *)tNumber);
}

- (id)setTNumber:(const char *)tN
{
    if (tNumber != NULL) free(tNumber);
    tNumber = NXCopyStringBuffer(tN);
    return self;
}

- (const char *)tDate
{
    return ((const char *)tDate);
}

- (id)setTDate:(const char *)tD
{
    tDate = NXUniqueString(tD);
    return self;
}

- (const char *)tMemo
{
    return ((const char *)tMemo);
}

- (id)setTMemo:(const char *)tM
{
    if (tMemo != NULL) free(tMemo);
    tMemo = NXCopyStringBuffer(tM);
    return self;
}

- (double)tAmount
{
    return ((double)tAmount);
}

- (id)setTAmount:(double)tA
{
    char                buf[256];

    tAmount = tA;
    if (tDebit != NULL) free(tDebit);
    if (tCredit != NULL) free(tCredit);
    if (tAmount >= 0) {
	sprintf(buf, "%.2f", tAmount);
	tCredit = NXCopyStringBuffer(buf);
	tDebit = "";
    } else {
	sprintf(buf, "%.2f", -tAmount);
	tDebit = NXCopyStringBuffer(buf);
	tCredit = "";
    };
    return self;
}

- (const char *)tBalance
{
    return (tBalance);
}

- (id)setTBalance:(const char *)tB
{
    double              ttA;

    if (tBalance != NULL) free(tBalance);
    tBalance = NXCopyStringBuffer(tB);
    sscanf(tB, "%lf", &ttA);
    tIBalance = ttA;
    return self;
}

- (const char *)tDebit
{
    return tDebit;
}

- (id)setTDebit:(const char *)tD
{
    double              ttA;

    sscanf(tD, "%lf", &ttA);
    ttA = -ttA;
    [self setTAmount:ttA];
    return self;
}

- (const char *)tCredit
{
    return tCredit;
}

- (id)setTCredit:(const char *)tC
{
    double              ttA;

    sscanf(tC, "%lf", &ttA);
    [self setTAmount:ttA];
    return self;
}


- source:aSource didReadRecord:(unsigned)record
{
    recordManager = aSource;
    if (![recordManager isMemberOf:[IXRecordManager class]])
	recordManager = [aSource delegate];

    runTimeHandle = record;
    return self;
}

- source:aSource willWriteRecord:(unsigned)record
{
    recordManager = nil;
    if (runTimeHandle == 0) runTimeHandle = record;
    return self;
}

- (id)copy
{
    return[super copy];
}

- (id)invertCopy
{
    id                  foo;

    foo = [super copy];
    [foo setTAmount:-[foo tAmount]];
    return foo;
}

- (id)becomeVoided:(id)v
{
    char                buf[64];

    if (tNumber != NULL) free(tNumber);
    tNumber = NXCopyStringBuffer("void");
    [self setTDate:[[NXApp delegate] todaysDate]];
    strcat(buf, "Void of #");
    strcat(buf,[v tNumber]);
    if (tMemo != NULL) free(tMemo);
    tMemo = NXCopyStringBuffer(buf);
    [self setTAmount:-[v tAmount]];
    return self;
}

@end
